using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.UI;

public class PlacementSystem : MonoBehaviour
{
    [SerializeField] private InputManager inputManager;
    [SerializeField] private Grid grid;

    [SerializeField] private GameObject mouseIndicator, gridVisualization, buildingParent;
 
    [SerializeField] private ObjectsDatabaseSO database;

    private GridData floorData, furnitureData;

    [SerializeField] private PreviewSystem previewSystem;
    [SerializeField] private ObjectPlacer objectPlacer;
    [SerializeField] private AudioSource source;

    private IBuildingState buliderState;


    [SerializeField] private Transform UIBtnParent;
    [SerializeField] private GameObject UIBtnPrefb;
    [SerializeField] private GameObject UIBtnRemove;


    void Start()
    {
        int childCount = UIBtnParent.childCount;
        int count = database.objectsData.Count;
        GameObject newObj = null;
        for (int i = 0; i < count; i++)
        {
            if (i < childCount)
            {
                newObj = UIBtnParent.GetChild(i).gameObject;
            }
            else
            {
                newObj = GameObject.Instantiate(UIBtnPrefb);
            }

            ObjectData data = database.objectsData[i];
            newObj.transform.parent = UIBtnParent;
            newObj.GetComponent<Button>().onClick.AddListener(() => { StartPlacement(data.ID); });
            newObj.transform.Find("Image").GetComponent<Image>().sprite = data.Icon;
        }

        UIBtnRemove.GetComponent<Button>().onClick.AddListener(StartRemoving);

        inputManager.OnHover += PlaceStructureOnHover;
        floorData = new();
        furnitureData = new();
        StopPlacement();
    }


    public void StartRemoving() //开始清除
    {
        StopPlacement();
        gridVisualization.SetActive(true);

        buliderState = new RemovingState(grid, previewSystem, floorData, furnitureData, objectPlacer, source);
        buliderState.StartState();

        inputManager.OnClicked += PlaceStructure;
        inputManager.OnExit += StopPlacement;
    }

    private void StartPlacement(int ID) //开始放置
    {
        StopPlacement();

        buliderState =
            new PlacementState(grid, previewSystem, database, floorData, furnitureData, objectPlacer, source);
        buliderState.StartState(ID);

        gridVisualization.SetActive(true);
        inputManager.OnClicked += PlaceStructure;
        inputManager.OnExit += StopPlacement;
    }

    private void StopPlacement() //结束放置
    {
        if (buliderState == null)
            return;

        buliderState.EndState();
        buliderState = null;

        gridVisualization.SetActive(false);
        inputManager.OnClicked -= PlaceStructure;
        inputManager.OnExit -= StopPlacement;
    }

    private void PlaceStructure() //放置物品
    {
        if (!CheckPlaceStructure(out Vector3Int endCellPos))
            return;

        buliderState.OnAction(endCellPos);
    }

    private void PlaceStructureOnHover() //放置物品 标记位置 update
    {
        if (!CheckPlaceStructure(out Vector3Int endCellPos))
            return;
    }

    private bool CheckPlaceStructure(out Vector3Int endPos) //放置物品 位置
    {
        endPos = Vector3Int.zero;

        if (inputManager.IsPointerOverUI())
            return false;

        Vector3 mousePosition = inputManager.GetSelectedMapPosition();
        Vector3Int gridPosition = grid.WorldToCell(mousePosition);
        mouseIndicator.transform.position = mousePosition;

        endPos = gridPosition;

        if (buliderState != null)
        {
            return buliderState.UpdateState(gridPosition);
        }

        return true;
    }
}